<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/7
 * Time: 10:37
 */

namespace api\storage\model;


use think\Model;

class PurchaseDetailModel extends Model
{
    protected $table = 'iwh_purchase_detail';
    protected $pk = 'uuid';

    public function Reels()
    {
        return $this->hasOne('ReelsModel', 'reel_id', 'reel_id', 'r', 'left')->bind(['reel_count' => 'count', 'status']);
    }


    /**
     * 统计摸某个物料最多的数量的信息，（统计学概率确定最小包装数量）
     *
     * @param string $subMaterielId
     * @return array
     */
    public function countMaterielNumber($subMaterielId = ''){
       return $this->where('sub_materiel_id',$subMaterielId)
            ->group('count')
            ->order('count(count) desc')
            ->limit(1)
            ->value('count');
    }

    /**
     * 查询reelId 所对应的物料号
     *
     * @param $reelId
     * @return mixed
     */
    public function findMaterielIdByReelId($reelId){
        return $this->where('reel_id',$reelId)->value('sub_materiel_id');
    }

    /**
     * 根据reelIds查询站位
     *
     * @param $reelIds
     * @param $storageUuid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryReels($reelIds,$storageUuid){
        return $this->alias('pd')
            ->field('pd.*,r.uuid as reel_uuid,r.container_code,r.count as reel_count,r.storage_uuid,r.seat_code')
            ->join(['iwh_reels'=>'r'],"r.reel_id = pd.reel_id and r.storage_uuid = '$storageUuid'",'left')
            ->whereIn('pd.reel_id',$reelIds)
            ->select();
    }

    /**
     *   查询reelId 所对应的
     *
     * @param $reelId
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findByReelId($reelId){
        return $this->where('reel_id',$reelId)->find();
    }

    /**根据reelId 获取数量
     * @param $reelId
     * @param $storageUuid
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findCountByReelId($reelId, $storageUuid)
    {
        $where = [
            'a.reel_id'=>$reelId,
        ];
        $reelInfo = $this->alias('a')->field('a.*,r.status,r.count as reel_count')
            ->join(['iwh_reels' => 'r'], "r.reel_id = a.reel_id and storage_uuid = '$storageUuid' and status = '1'", 'left')
            ->where($where)
            ->find();
        if (!count($reelInfo)) {
            return null;
        }
        if ($reelInfo['status'] == 1) {
            $count = $reelInfo['reel_count'];
        } else {
            $count = $reelInfo['count'];
        }
        return $count;
    }

    /**
     * 更新采购单的数量
     *
     * @param $reelId
     * @param $count
     * @return false|int
     */
    public function updateReelsCount($reelId, $count)
    {
        return $this->save(['count' => $count], ['reel_id' => $reelId]);
    }

    public function getMaterielId($reel_id = '', $storage_uuid = '')
    {
        $where = ['reel_id' => $reel_id, 'storage_uuid' => $storage_uuid];
        $result = $this->where($where)->field('sub_materiel_id')->find();
        return $result;
    }

}